package org.dzq.designmode.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Composite
 * @Package: org.dzq.designmode.composite
 * @author: 越
 * @date: 2023/6/17 16:34
 * @Description: 树枝节点
 */
public class Composite extends Component{

    List<Component> componentList;

    public Composite(String name) {
        super(name);
        this.componentList = new ArrayList<>();
    }


    @Override
    public void printTree() {
        System.out.println(this.name);
        for (Component component : componentList){
            System.out.println("\n");
            component.printTree();
        }
    }

    @Override
    public boolean addChild(Component component){
        return componentList.add(component);
    }

    @Override
    public boolean removeChild(Component component){
        return componentList.remove(component);
    }

    @Override
    public Component getChild(int index){
        return componentList.get(index);
    }
}
